package com.k.demo.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.k.demo.entity.CustomerUser;
import com.k.demo.entity.PointDetail;
import com.k.demo.entity.RedBag;
import com.k.demo.service.CustomerUserService;
import com.k.demo.service.PointDetailService;

@RestController
@RequestMapping("/pointdetail")
public class PointDetailController {

	@Autowired
	private PointDetailService pointDetailService;
	@Autowired
	private CustomerUserService customerUserService;
	
	/**
	 * 获取当前用户的所有积分详情
	 */
	@RequestMapping("/all")
	public Object getAll(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser loginuser = (CustomerUser) request.getSession().getAttribute("loginUser");
		
		if (loginuser==null) {
			map.put("err", "您还没有登录");
		}else {
			map.put("loginUser", loginuser);
			map.put("list", pointDetailService.getAll(loginuser.getUser_id()));
		}
		return map;
	}
	
	/**
	 * 插入积分详情
	 */
	@RequestMapping("/insertPointDetail")
	public Object insertPointDetail(@RequestBody PointDetail pointDetail,HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser loginuser = (CustomerUser) request.getSession().getAttribute("loginUser");
		
		if (loginuser==null) {
			map.put("err", "您还没有登录");
		}else {
			map.put("loginUser", loginuser);
			pointDetail.setUser_id(loginuser.getUser_id());
			map.put("msg", pointDetailService.insertPointDetail(pointDetail));
			if (pointDetail.getPoint_change_id()==51) {
				//更改用户新积分数量
				loginuser.setUser_point(loginuser.getUser_point()+pointDetail.getPoint_change_amount());
			}else {
				//更改用户新积分数量
				loginuser.setUser_point(loginuser.getUser_point()-pointDetail.getPoint_change_amount());
			}
			//更改用户表
			customerUserService.updateCustomerUser(loginuser);
			//更改session中用户
			request.getSession().setAttribute("loginUser", loginuser);
		}
		return map;
	}
	
	/**
	 * 显示所有用户所有积分分页
	 */
	@RequestMapping("/getalluserpoint")
	public Object getAllUserPoint(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("allList", customerUserService.getAlltWithPage((int)map.get("pageNum"), (int)map.get("pageSize")));
		
		return rmap;
	}
	
	/**
	 * 查看当前用户的积分详情
	 */
	@RequestMapping("/getoneuserpoint")
	public Object getOneUserPoint(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("allList", pointDetailService.getAll((int)map.get("userid")));
		
		return rmap;
	}
	
	/**
	 * 积分明细组合查询
	 */
	@RequestMapping("/searchbypointdetail")
	public Object searchWithPage(@RequestBody PointDetail pointDetail, @RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("searchResyltWithPage", pointDetailService.searchResultWithPage(pageNum, pageSize, pointDetail));		
		
		return rmap;
		
	}
	/**
	 * 根据传入的用户对象进行查询
	 */
	@RequestMapping("/searchbyuserinfo")
	public Object searchByUserInfoWithPage(@RequestBody CustomerUser customerUser, @RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("searchResyltWithPage", customerUserService.getSearchResutlttWithPage(pageNum, pageSize, customerUser));		
		
		return rmap;
		
	}
	
	/**
	 * 从session获取当前的用户
	 */
	@RequestMapping("/getByNowUser")
	public Object getByNowUser(HttpSession session) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		CustomerUser customerUser = (CustomerUser) session.getAttribute("loginUser");
		
		int userid = customerUser.getUser_id();
		
		rmap.put("nowUser", pointDetailService.getAll(userid));
		
		return rmap;
	}
	
}	
